<?php
/**
 * Created by PhpStorm.
 * User: yhb
 * Date: 2018/10/23
 * Time: 9:28
 */
/**
 * 首先展示待编辑的数据
 * 修改之后保存修改
 */
//判断请求时get还是Post
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];
        $con = mysqli_connect('localhost', 'root', 'root');
        if (!$con) {
            die('连接失败' . mysqli_connect_error());
        }
        mysqli_select_db($con, 'blog');
        mysqli_set_charset($con, 'utf8');
        $sql = "select * from blog_type where type_id=$id";
        $result = mysqli_query($con, $sql);
        if (!$result) {
            //返回的时最近一次的错误
            echo '查询失败' . mysqli_error($con);
        } else {
            //因为id时唯一的，所以最终智能查询到一条数据
            $row = mysqli_fetch_assoc($result);
        }
    }
}else if($_SERVER['REQUEST_METHOD'] == 'POST'){
    //获取修改后的类别名称
    $type_name=$_POST['type_name'];
    $type_id=$_POST['type_id'];
    $con=mysqli_connect('localhost','root','root');
    if (!$con){
        die('连接失败'.mysqli_connect_error());
    }
    mysqli_select_db($con,'blog');
    mysqli_set_charset($con,'utf8');
    $sql="update blog_type set type_name='$type_name' where type_id=$type_id";
    $result=mysqli_query($con,$sql);
    if (!$result){
        echo '保存失败';
    }else{
        header("Location:blogtype_list.php");
    }
    mysqli_close($con);
}

?>

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>发表博客</title>
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .box {
            width: 500px;
            margin-top: 100px;
        }

    </style>
</head>
<body>
<div class="container box">
    <a href="blogtype_list.php">返回</a>
    <form class="form-horizontal" action="blogtype_edite.php" method="post">
        <input type="hidden" value="<?php echo $row['type_id'] ?>" name="type_id">
        <div class="form-group">
            <label for="inputEmail3" class="col-sm-4 control-label">类别名称</label>
            <div class="col-sm-8">
                <input type="text" value="<?php echo $row['type_name'] ?>"
                       class="form-control" id="type_name" name="type_name" placeholder="请输入类别名称">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-4 col-sm-10">
                <button type="submit" class="btn btn-default">保存</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>
